package com.mew.common.dc.db;

import com.mew.common.dc.mapper.InoutLobbyMapper;
import com.mew.common.dc.mapper.UserMapper;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.enm.ExitType;
import com.mew.common.entity.user.User;
import com.mew.common.rest.bo.MewSession;
import com.zebra.common.OnlineType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;


@Service
@Slf4j
public class LoginoutService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @Autowired
    private AucRedisService aucRedisService;

    //---------payler-loginOut---------------
    public MewSession login(MewSession session, long userId, String mac, String ip, int devType){

        if (null != session && !session.checkPlaying()){
            doExitRoom(session,  ExitType.Repeat);
        }

        loginUpdate(userId, mac, ip, devType);

        session = null == session ? new MewSession() : session;
        session.uid = userId;
        session.tk = aucRedisService.getToken();
        session.loc = 0;
        session.onl = OnlineType.Unbound.value();
        aucRedisService.webLogin(session);

        return session;
    }

    //--1, 切换账号退出--2，Redis Session过期退出---3，顶号退出-----删除session by redis------
    public void logout(long userId, int exitType){
        ExitType et = Optional.ofNullable(ExitType.getByCode(exitType)).orElse(ExitType.Expires);
        MewSession session = aucRedisService.getSession(userId);
        if (null == session){
            userService.deleteByUserId(userId);
            return;
        }

        if (!session.checkPlaying()){
            doExitRoom(session,  et);
            aucRedisService.removeSession(userId);
            userService.deleteByUserId(userId);
        }

    }




    private void loginUpdate(long userId, String mac, String ip, int devType){
        User user = new User();
        user.setId(userId);
        user.setLastMac(mac);
        user.setLastIp(ip);
        user.setLastDevicesType(devType);
        user.setLastLoginTime(new Date());
        userMapper.updateById(user);
    }


    //--------room-inout-------------------------
    public void exitRoom(long userId, int exitType){
        MewSession session = aucRedisService.getSession(userId);
        doExitRoom(session,  ExitType.getByCode(exitType));
    }

    public void doExitRoom(MewSession session,  ExitType exitType){
        log.info("Exit s: {} ", null == session ? "" : session.toString());
        if (session == null){
            log.info("Exit session is null u: {} roomId {} ", session.uid, session.rid);
            return ;
        }

//        if (session.rioid == -9){
//           aucRedisService.exitGame(session.uid);
//        }

        exitType = null == exitType ? ExitType.Exit : exitType;
        int roomId = session.rid;
        exitRoomInOut(session,   exitType);


        aucRedisService.exitGame(session.uid);
        session.exitGame();
    }



    public void exitRoomInOut(MewSession session,  ExitType exeStateType){
        if (null == session || session.rioid == -9){
            return;
        }

        Date date = new Date();


//        if (!ExitType.Normal.equals(exeStateType)){
//            LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<InoutRoom>()
//                    .eq(InoutRoom::getUserId, inoutRoom.getUserId())
//                    .eq(InoutRoom::getState, ExeStateType.Unfinished.code());
//            List<InoutRoom> list = inoutRoomMapper.selectList(queryWrapper);
//            if (null == list || list.isEmpty()){
//                return;
//            }
//
//            list.forEach(ir -> {
//                ir.setOutTime(date);
//                ir.setRemark(ExitType.Overdue.desc() + "-P");
//                ir.setState(ExeStateType.Executed.code());
//                ir.setOutGoldNumber(profile.getGoldNumber());
//                ir.setOutLevelId(profile.getUserLevel());
//                inoutRoomMapper.updateById(entity);
//            });
//        }

    }




}
